<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    title="编辑部门"
    :visible="showDialog"
    width="60%"
    @close="handelClose"
    @open="handleOpen"
  >
    <!-- 表单 -->
    <el-form
      ref="form"
      :model="deptform"
      :rules="deptFromRules"
      label-width="100px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptform.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptform.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="deptform.manager"
          placeholder="请选部门负责人"
          style="width: 80%"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
          <el-option label="ikun" value="ikun" />
          <el-option label="邢芮豪" value="邢芮豪" />
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptform.introduce" type="textarea" />
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <template #footer>
      <span slot="dialog-footer">
        <el-button type="primary" @click="handelEditDept">确定</el-button>
        <el-button @click="handelClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {
  getManagerListAPI,
  editDeptAPI,
  getDeptDetailAPI
} from '@/api/departments'
export default {
  name: 'EditDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    deptList: {
      type: Array,
      default: () => []
    },
    deptItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      // 在编辑的时候，部门名称无需与自己比较
      //   思路：从deptlist中排除自己
      const isSame = this.deptList
        .filter((obj) => obj.id !== this.deptItem.id) // filter()就是在排除自己
        .some((item) => item.name === value)
      isSame ? callback(new Error(`【${value}】部门已存在`)) : callback()
    }
    // 自定义部门编码查重
    const validateCode = (rule, value, callback) => {
      const isSame = this.deptList
        .filter((obj) => obj.id !== this.deptItem.id) // filter()就是在排除自己
        .some((item) => {
          item.code === value
        })
      // 三元判断是否已存在
      isSame ? callback(new Error(`【${value}】编码已存在`)) : callback()
    }
    return {
      // 部门表单
      deptform: {
        name: '',
        introduce: '',
        code: '',
        manager: ''
      },
      //   部门表单校验规则
      deptFromRules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            triger: ['change', 'blur']
          },
          {
            min: 1,
            max: 50,
            massage: '长度在1到50个字符之间',
            triger: 'blur'
          },
          {
            // 调用自定义校验函数
            validator: validateName,
            triger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            triger: ['change', 'blur']
          },
          {
            min: 1,
            max: 50,
            massage: '长度在1到50个字符之间',
            triger: ['change', 'blur']
          }, // 调用自定义校验函数
          {
            validator: validateCode,
            triger: ['change', 'blur']
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            triger: ['change', 'blur']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            triger: ['change', 'blur']
          },
          {
            min: 1,
            max: 300,
            massage: '长度在1到300个字符之间',
            triger: ['change', 'blur']
          }
        ]
      },
      // 部门负责人列表
      managerList: []
    }
  },
  methods: {
    // 编辑部门
    handelEditDept() {
      //  整体校验
      // eslint-disable-next-line space-before-function-paren
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        // 发起请求
        await editDeptAPI(this.deptform)
        // 关闭弹框
        this.handelClose()
        // 直接获取父组件实例调用获取部门列表方法
        // this.$parent.getDeptList()
        // 子传父通知父组件，再拉取一次部门列表
        this.$emit('edit-dept-success')
      })
    },
    // 获取部门负责人列表
    async handleOpen() {
      const res = await getDeptDetailAPI(this.deptItem.id)
      //   回显：给v-model绑定的属性直接赋值
      this.deptform = res.data
      // 防止无效请求
      if (this.managerList.length) return
      const resp = await getManagerListAPI()
      this.managerList = resp.data
    },
    // 关闭弹窗
    handelClose() {
      // 触发自定义事件
      this.$emit('update:showDialog', false)
      // 重置表单
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped>
.el-input,
.el-textarea {
  width: 80%;
}
</style>
